package DateTime::Cursor::TestScaffolding::Utilities;

use warnings;
use strict;
use Carp;

use version; our $VERSION = qv('0.0.1');
use base qw( Exporter );
our @EXPORT = qw( nop_tests pretty_print cmp_cursors str_cmp_dt_to_cursor num_cmp_dt_to_cursor );

use Test::More;
#use Test::More qw(no_plan);
use Data::Dumper;
use DateTime::Format::Mail;
use DateTime::TimeZone;
use DateTime::Cursor;

sub nop_tests {
  my ($message, $number_of_nops) = ($_[0], $_[1]);
  for(my $i = 0; $i < $number_of_nops; $i++) {
    ok(1, "nop test $message : $i");  
  }
  return;
}

sub pretty_print {
  my $param = $_[0];
  if (! defined $param) {
    return;
  }
  my $string;
  eval {
    $string = join('', (ref $param), '(', (ident $param), ')=', "$param");
  };
  return ((defined $@ and "" ne "$@") ? "$param" : $string);
}

# 5 tests per call
sub cmp_cursors {
  my ($cursor_control, $cursor_test, $expected_cmp, $message) = @_;
  ok(defined $cursor_control);
  ok(defined $cursor_test);
  $message .= join('', " using cmp_cursors(",
            pretty_print($cursor_control),
            ',',
            pretty_print($cursor_test),
            ',',
            $expected_cmp,
            ')');
  my $control_pivot = $cursor_control->get_pivot();
  ok(defined $control_pivot, $message);
  my $test_pivot = $cursor_test->get_pivot();
  ok(defined $test_pivot, $message);
  my $found_cmp = DateTime->compare_ignore_floating($control_pivot, $test_pivot);
  is($found_cmp, $expected_cmp, $message .=
     join('', ' from DateTime->compare_ignore_floating(',
	  $control_pivot->iso8601(),
	  ',',
	  $test_pivot->iso8601(),
	  ')'));
  return; 
}

# 1 test per call
sub str_cmp_dt_to_cursor {
  my ($dt, $iter, $expected_cmp, $formatter, $message) = @_;
  $message .= join(',', " with str_cmp_dt_to_cursor", $dt, $iter, $expected_cmp, $formatter);
  my $str_cmp = ($expected_cmp < 0 ? 'lt' : ($expected_cmp == 0 ? 'eq' : 'gt'));
  if("" eq $formatter) {
    cmp_ok("$iter", $str_cmp, $dt->iso8601(), "$message : iter $str_cmp dt");
  } else {
    my $special_cmp = ($expected_cmp == 0 ? "eq" : "ne");
    cmp_ok("$iter", $special_cmp, $formatter->format_datetime($dt), "$message : iter $special_cmp dt");
  }
}

# 1 test per call
sub num_cmp_dt_to_cursor {
  my ($dt, $iter, $expected_cmp, $message) = @_;
  $message .= join(',', " with num_cmp_dt_to_cursor", $dt, $iter, $expected_cmp);
  my $num_cmp = ($expected_cmp < 0 ? '<' : ($expected_cmp == 0 ? '==' : '>'));
  cmp_ok($iter->get_pivot()->epoch(), $num_cmp, $dt->epoch(), "$message : iter $num_cmp dt");
  return;
}

1;
